package com.deanlib.cabinet.base;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.deanlib.cabinet.R;
import com.deanlib.cabinet.data.network.Response;
import com.deanlib.plantern.utils.AppUtils;
import com.elvishew.xlog.XLog;

import java.util.Optional;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by Dean on 2023/2/18
 */
public abstract class BaseService extends Service implements IPopupAuxiliary,INetworkAuxiliary {
    protected static String CHANNEL_ID = "com.deanlib.cabinet.service";
    protected static String CHANNEL_SERVICE = "Service";

    protected NetworkAuxiliary networkAuxiliary;
    private PopupAuxiliary popupAuxiliary;
    private CompositeDisposable compositeDisposable;

    public static String TAG = "BaseService";

    public int getNotifyId(){
        return (int) System.currentTimeMillis();
    }

    public String getNotifyTicker(){
        return getClass().getSimpleName();
    }

    public String getNotifyTitle(){
        return getClass().getSimpleName();
    }

    public String getNotifyContent(){
        return getClass().getSimpleName();
    }

    public String getNotifyBigContent(){
        return getNotifyContent();
    }

    public PendingIntent getNotifyPendingIntent(){
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        TAG = getClass().getSimpleName();
        XLog.i(TAG + " > onCreate");
        popupAuxiliary = new PopupAuxiliary(this);
        networkAuxiliary = new NetworkAuxiliary(popupAuxiliary);

        Notification notification = AppUtils.createNotification(this,
                getNotifyId(), CHANNEL_ID, CHANNEL_SERVICE,
                R.mipmap.icon, R.mipmap.icon, getNotifyTicker(),
                getNotifyTitle(), getNotifyContent(), getNotifyBigContent(), getNotifyPendingIntent());

        startForeground(getNotifyId(), notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        XLog.i(getClass().getSimpleName() + " > onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void toast(String msg) {
        if (popupAuxiliary !=null) {
            popupAuxiliary.toast(msg);
        }
    }

    @Override
    public void loading(String title, String content) {
        if (popupAuxiliary !=null) {
            popupAuxiliary.loading(title, content);
        }
    }

    @Override
    public void loaded() {
        if (popupAuxiliary !=null) {
            popupAuxiliary.loaded();
        }
    }

    @Override
    public void networkError() {
        if (popupAuxiliary !=null) {
            popupAuxiliary.networkError();
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next, error);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error, Action complete) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next, error, complete);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public void dispose() {
        if (networkAuxiliary !=null){
            networkAuxiliary.dispose();
        }
    }

    protected void addDisposable(Disposable disposable){
        if (compositeDisposable == null){
            synchronized (this){
                if (compositeDisposable == null){
                    compositeDisposable = new CompositeDisposable();
                }
            }
        }
        compositeDisposable.add(disposable);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        XLog.i(TAG + " > onDestroy");
        if (compositeDisposable!=null) {
            compositeDisposable.dispose();
        }
        stopForeground(true);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        XLog.i(TAG + " > onBind");
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        XLog.i(TAG + " > onUnbind");
        return super.onUnbind(intent);
    }
}
